//
//  Exception.cpp
//  Simulation
//
//  Created by Christopher Kemsley on 2/27/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include "Exception.hpp"
#include <iostream>



NamespaceBegin


std::ostream&	operator<<	( std::ostream & out , Exception const & exception )
	{
	out << "********** Exception **********\n" ;
	
	std::vector<ExceptionTrace>::const_iterator	here	=	exception.mTrace.begin() ;
	std::vector<ExceptionTrace>::const_iterator	end		=	exception.mTrace.end() ;
	while ( here < end )
		{
		ExceptionTrace const & t = *(here++) ;
		
		out << "	" << t << "\n" ;
		}
	
	return out ;
	}


std::ostream&	operator<<	( std::ostream & out , ExceptionTrace const & t )
	{
	// Find the name:
	size_t pos = t.file.rfind("/") ;
	if ( pos != std::string::npos )
		out << t.file.substr(pos+1) ;
	else
		out << t.file ;
	
	// Line number:
	out << ":" << t.line ;
	
	// Domain:
	out << " - " << t.domain ;
	
	// Description:
	out << " (" << t.description << ")" ;
	
	return out ;
	}


NamespaceEnd







using namespace Sim ;






Exception :: Exception	( std::string const & inDomain , std::string const & inDescription , char const * inFile , int inLine )
:	mTrace	( 1 , ExceptionTrace(inDomain,inDescription,inFile,inLine) )
	{
	
	}
Exception :: Exception	( Exception const & copy )
:	mTrace ( copy.mTrace )
	{
	
	}
Exception :: ~Exception	()
	{
	
	}

void	Exception :: add	( std::string const & inDomain , std::string const & inDescription , char const * inFile , int inLine )
	{
	mTrace . push_back ( ExceptionTrace(inDomain,inDescription,inFile,inLine) ) ;
	}


std::vector<ExceptionTrace> const&		Exception :: trace ()
	{
	return mTrace ;
	}






ExceptionTrace :: ExceptionTrace	( std::string const & inDomain , std::string const & inDescription , char const * inFile , int inLine )
:	domain		( inDomain )
,	description	( inDescription )
,	file		( inFile )
,	line		( inLine )
	{
	
	}
ExceptionTrace :: ExceptionTrace	( ExceptionTrace const & copy )
:	domain		( copy.domain )
,	description	( copy.description )
,	file		( copy.file )
,	line		( copy.line )
	{
	
	}
ExceptionTrace :: ~ExceptionTrace	()
	{
	
	}

void	ExceptionTrace :: operator=	( ExceptionTrace const & copy )
	{
	domain = copy.domain ;
	description = copy.description ;
	file = copy.file ;
	line = copy.line ;
	}
